package com.ymatou.iapi.sellerproduct.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.iapi.sellerproduct.parameter.ProdTplBean;
import com.ymt.core.tool.Logger;
import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.database.sqlwapper.YmtProductDescExtraWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class SaveProductTemplateCall extends YmatouAutoTestCall{
	private static String opurl ="/api/SellerProduct/SaveProductTemplate";
	
	//private static 
	
	public SaveProductTemplateCall() {
		super(EnvSetup.getSystemUrl("sellerproduct_iapi"),opurl,"POST","JSON");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	
	public ProdTplBean saveTemplate(int sellerId,int templateTypeId) throws JSONException{
		return saveTemplate(sellerId,templateTypeId,null);
	}
	
	
	public ProdTplBean saveTemplate(int sellerId,int templateTypeId,Map<String, Object> updateMap) throws JSONException{
		Map<String, Object> map = MapUtil.hashMap();
		
		if (updateMap!=null && updateMap.size()>0){map.putAll(updateMap);}
		
		map.put("SellerId", sellerId);
		map.put("TemplateType", templateTypeId);
		String p1 = "http://pic1.ymatou.com/G01/shangou/M00/31/92/rBB"+Calendar.getInstance().getTimeInMillis()+".jpg";
		String p2 = "http://pic1.ymatou.com/G01/shangou/M00/22/92/rBB"+Calendar.getInstance().getTimeInMillis()+".jpg";
		map.put("PicUrls", new JSONArray(Arrays.asList(p1,p2)));
		
		JSONObject jsonData = callAndGetReturnData(map);
		JSONObject requestData = jsonData.getJSONObject("JsonBeanRequest");
		
		ProdTplBean prodTplBean = new ProdTplBean();
		prodTplBean.setTemplateId(jsonData.getJSONObject("Data").getInt("ProductTemplateId"));
		prodTplBean.setTemplateSellerId(requestData.getInt("SellerId"));
		prodTplBean.setTemplateName(requestData.getString("TemplateName"));
		prodTplBean.setTemplateDesc(requestData.getString("Description"));
		prodTplBean.setTemplateType(requestData.getInt("TemplateType"));
		
		JSONArray picArray = requestData.getJSONArray("PicUrls");
		List<String> picList = new ArrayList<>();
		for(int i=0;i<picArray.length();i++){picList.add(picArray.getString(i));}
		prodTplBean.setPicUrls(picList);
		
		
		return prodTplBean;
		
	
	}
	
	public int deleteTemplate(int sellerId){
		Logger.comment("删除模板   sellerID:"+sellerId);
		YmtProductDescExtraWapper ymtProductDescExtraWapper = new YmtProductDescExtraWapper();
		return ymtProductDescExtraWapper.deleteTemplate(sellerId);
	}
	
	public static void main(String[] args) throws Exception {
		//new SaveProductTemplateCall().deleteTemplate(3383);
		ProdTplBean aBean= new SaveProductTemplateCall().saveTemplate(3383,3,null);
		int c=1;
		
	}
}
